package com.pronosticnews.client.admin.view.editSaison;

import java.util.List;

import com.allen_sauer.gwt.dnd.client.DragEndEvent;
import com.allen_sauer.gwt.dnd.client.DragHandler;
import com.allen_sauer.gwt.dnd.client.DragStartEvent;
import com.allen_sauer.gwt.dnd.client.VetoDragException;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.pronosticnews.client.admin.resources.PronosticnewsAdminMessages;
import com.pronosticnews.client.admin.resources.PronosticnewsAdminResources;
import com.pronosticnews.client.util.ClubWidget;
import com.pronosticnews.client.util.dnd.DragClubUtil;
import com.pronosticnews.client.util.dnd.DragUtil;
import com.pronosticnews.client.util.dnd.DraggableFlexTable;
import com.pronosticnews.client.util.dnd.FlexTableRowDragController;
import com.pronosticnews.client.util.dnd.FlexTableRowDropController;
import com.pronosticnews.shared.model.ClubDto;
import com.pronosticnews.shared.model.SaisonDto;

public class EditSaisonViewImpl extends Composite implements EditSaisonView {

	interface EditSaisonUiBinder extends UiBinder<Widget, EditSaisonViewImpl> {
	}

	@UiField(provided=true)
	AbsolutePanel panel_dnd_clubs;

	@UiField(provided=true)
	DraggableFlexTable table_clubs;

	@UiField(provided=true)
	DraggableFlexTable table_clubs_choisis;

	private Presenter presenter;

	@UiField
	TextBox txtNom;

	@UiField
	Label lblChampionnat;

	@UiField
	Label lblTitle;

	@UiField
	HorizontalPanel panNouveau;

	@UiField
	HorizontalPanel panModifier;

	@UiField
	VerticalPanel containerForm;

	@UiField
	DialogBox clubsParticipantsBox;

	@UiField(provided = true)
	PronosticnewsAdminResources res;

	PronosticnewsAdminMessages messages;

	private boolean alreadyGetClubs = false;

//	private HashMap<String, ClubDto> mapClubParticipants;

	public EditSaisonViewImpl() {
//		mapClubParticipants = new HashMap<String, ClubDto>();
		
		initClubsDragnDrop();
		
		messages = GWT.create(PronosticnewsAdminMessages.class);
		res = GWT.create(PronosticnewsAdminResources.class);
		res.cssFormEdition().ensureInjected();
		initWidget(GWT.<EditSaisonUiBinder> create(EditSaisonUiBinder.class)
				.createAndBindUi(this));
		lblTitle.addStyleName(res.cssFormEdition().title_creation());

		
	}

	private void initClubsDragnDrop() {
		panel_dnd_clubs = new AbsolutePanel();
		
		DragUtil dragClubUtil = new DragClubUtil();
		
		// instantiate our drag controller
		FlexTableRowDragController tableRowDragController = new FlexTableRowDragController(
				panel_dnd_clubs,dragClubUtil);
		tableRowDragController.addDragHandler(new DragHandler() {

			@Override
			public void onPreviewDragStart(DragStartEvent event)
					throws VetoDragException {
			}

			@Override
			public void onPreviewDragEnd(DragEndEvent event)
					throws VetoDragException {
			}

			@Override
			public void onDragStart(DragStartEvent event) {
			}

			@Override
			public void onDragEnd(DragEndEvent event) {
			}
		});

		table_clubs = new DraggableFlexTable(tableRowDragController);
		table_clubs_choisis = new DraggableFlexTable(tableRowDragController);
		
		FlexTableRowDropController flexTableRowDropController1 = new FlexTableRowDropController(
				table_clubs_choisis, dragClubUtil);
		FlexTableRowDropController flexTableRowDropController2 = new FlexTableRowDropController(
				table_clubs, dragClubUtil);
		tableRowDragController
				.registerDropController(flexTableRowDropController1);
		tableRowDragController
				.registerDropController(flexTableRowDropController2);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void submitComplete() {
		txtNom.setValue("");
	}

	@Override
	public void setSaison(SaisonDto existingSaison) {
		txtNom.setText(existingSaison.getNom());
	}

	@Override
	public void setModeModification(boolean mode) {
		panNouveau.setVisible(!mode);
		panel_dnd_clubs.setVisible(!mode);
		panModifier.setVisible(mode);
		txtNom.setText("");
		if (mode) {
			lblTitle.setText(messages.modifier_saison());
			lblTitle.addStyleName(res.cssFormEdition().title_modif());
			lblTitle.removeStyleName(res.cssFormEdition().title_creation());
		} else {
			lblTitle.setText(messages.enregistrer_saison());
			lblTitle.addStyleName(res.cssFormEdition().title_creation());
			lblTitle.removeStyleName(res.cssFormEdition().title_modif());
		}
	}

	@UiHandler({ "btEnregistrer", "btModifier" })
	void onClick(ClickEvent event) {
		SaisonDto editSaison = new SaisonDto();
		editSaison.setNom(txtNom.getValue());
		ClubWidget clubWidget = null;
		for (int i = 0; i < table_clubs_choisis.getRowCount(); i++) {
			clubWidget = (ClubWidget) table_clubs_choisis.getWidget(i, 0);
			
//			ClubDto participant = mapClubParticipants.get(table_clubs_choisis.getText(i, 0));
			editSaison.getClubsParticipants().add(clubWidget.getClub());
		}
		
		presenter.submit(editSaison);
		setModeModification(false);
	}

	@UiHandler("btAnnuler")
	void doClickAnnuler(ClickEvent event) {
		setModeModification(false);
		presenter.annuleModification();
	}

	@UiHandler("bt_clubs_participants")
	void doClickBtClubsParticipants(ClickEvent event) {
		if (!alreadyGetClubs && table_clubs_choisis.getRowCount() == 0) {
			presenter.getClubs();
			alreadyGetClubs = true;
		}
		
		clubsParticipantsBox.center();
		clubsParticipantsBox.setPopupPositionAndShow(new PositionCallback() {			
			@Override
			public void setPosition(int offsetWidth, int offsetHeight) {
				clubsParticipantsBox.setPopupPosition(getAbsoluteLeft()-230, 150);
			}
		});
		
	}

	@UiHandler("btFermerClubsParticipants")
	void onCloseDialogClubsParticipants(ClickEvent event) {
		clubsParticipantsBox.hide();
	}

	@Override
	public void setNomChampionnat(String nom) {
		lblChampionnat.setText(nom);
	}

	@Override
	public void setClub(int i, List<ClubDto> result) {
		table_clubs.removeAllRows();
//		mapClubParticipants.clear();
		for (ClubDto clubDto : result) {
//			mapClubParticipants.put(clubDto.getNom(), clubDto);
			ClubWidget clubWidget = new ClubWidget(clubDto);
			table_clubs.addDraggableRow(clubWidget, 0);
//			table_clubs.addDraggableRow(clubDto.getNom(), 1);
		}
	}

	@Override
	public void onStop() {
		clubsParticipantsBox.hide();
//		mapClubParticipants.clear();
		table_clubs.removeAllRows();
		table_clubs_choisis.removeAllRows();
	}
}
